import requests
import json
from urllib import parse
import time

wd = "大数据"
# 对中文进行编码
wd = parse.quote(wd)
print(wd)

url = "https://m.weibo.cn/api/container/getIndex?containerid=100103type%3D60%26q%3D$1%26t%3D0&page_type=searchall&page=$2"

# 替换关键字
url = url.replace("$1", wd)

with open("data/weibo.txt", "w", encoding="utf-8") as file:
    for i in range(10):
        # 发起get请求
        result = requests.get(url.replace("$2", str(i)))

        # 获取返回的数据
        print(result.text)

        # 将json的字符串转换成json对象
        js = json.loads(result.text)

        data = js["data"]
        # 所有微博的数组
        cards = data["cards"]

        for card in cards:
            mblog = card["mblog"]

            id = mblog["id"]

            text = mblog["text"]
            user = mblog["user"]
            reposts_count = mblog["reposts_count"]
            comments_count = mblog["comments_count"]
            attitudes_count = mblog["attitudes_count"]

            screen_name = user["screen_name"]

            follow_count = user["follow_count"]
            followers_count = user["followers_count"]
            gender = user["gender"]

            line = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (
                id, reposts_count, comments_count, attitudes_count, screen_name, gender, follow_count, followers_count,
                text)

            file.write(line)
            file.write("\n")

            print(line)

            # 停一秒
            # time.sleep(1000)
